package com.liwu.publish.platform.web.common;

import com.liwu.publish.platform.common.exception.ErrorCode;
import com.liwu.publish.platform.common.exception.PublishException;
import com.liwu.publish.platform.common.response.ResStatus;
import com.liwu.publish.platform.common.response.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Auther: liwu
 * @Date: 2019/10/23 19:44
 * @Description:
 */
@EnableWebMvc
@ControllerAdvice
@Slf4j
public class GlobeExceptionHandler {

    //处理自定义的异常xx
    @ExceptionHandler(PublishException.class)
    @ResponseBody
    public Response customHandler(PublishException e, HttpServletRequest request, HttpServletResponse httpServletResponse){
        log.error("业务异常",e);
        return Response.error(e);
    }

    //其他未处理的异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Response exceptionHandler(Exception e, HttpServletRequest request, HttpServletResponse httpServletResponse){
        log.error("系统异常",e);
        return Response.error(ResStatus.UNEXPECTED_EXP.getCode(), ResStatus.UNEXPECTED_EXP.getMsg());
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseBody
    public Response pageNotFoundHandler(NoHandlerFoundException e, HttpServletRequest request, HttpServletResponse httpServletResponse){
        log.error("系404");
        return Response.error(ResStatus.PAGE_NOT_FOUND.getCode(), ResStatus.PAGE_NOT_FOUND.getMsg());

    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public Response pageNotFoundHandler(HttpRequestMethodNotSupportedException e, HttpServletRequest request, HttpServletResponse httpServletResponse){
        log.error("http 方法不支持",e);
        return Response.error(ResStatus.HTTP_METHOD_NOT_SUPPORTED.getCode(), ResStatus.HTTP_METHOD_NOT_SUPPORTED.getMsg());

    }

    /**
     * 拦截MethodArgumentNotValidException
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public Response handleBindException(MethodArgumentNotValidException e) {
        StringBuilder builder = new StringBuilder();
        e.getBindingResult().getFieldErrors().stream().forEach(x -> {
            builder.append(x.getField()).append(":").append(x.getDefaultMessage()).append(";");
        });
        log.error( "请求参数校验异常", e, builder.toString());
        return Response.error(ErrorCode.PARAM_ERROR.getCode(), builder.toString());
    }

    /**
     * 拦截BindException
     */
    @ExceptionHandler(BindException.class)
    @ResponseBody
    public Response handleBindException(BindException e) {
        StringBuilder builder = new StringBuilder();
        e.getFieldErrors().forEach(x -> builder.append(x.getField())
                .append(":")
                .append(x.getDefaultMessage())
                .append(";"));
        log.error("请求参数校验异常", e, builder.toString());
        return Response.error(ErrorCode.PARAM_ERROR.getCode(), builder.toString());
    }
}
